﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using DomainModel;

namespace Utils
{
    /// <summary>
    /// singleton class for initializing all static data. 
    /// </summary>
    public sealed class GlobalUtilities
    {
        private static GlobalUtilities _instance = new GlobalUtilities();

        private string _stripePublishableKey;

        private string _connectionString;
        private Boolean _debugMode;
        private Boolean _needTiming;
        private Boolean _minifyJsCssFiles;
        private string _minificationFilePathPrefix;
        private string _backgroundTaskExecutionKey;

        private Boolean _testEmail;
        private string _testEmailFrom;
        private string _testEmailTo;

        private string _emailServerHost;
        private string _emailCredentialUserName;
        private string _emailCredentialPassword;
        private string _email_BCC;
        private string _email_General;
        private string _email_NoReply;
        private string _email_Support;
        private string _email_DisplayName;

        private List<JobExp> _jobExpList;
        private List<JobType> _jobTypeList;
        private List<CoType> _coTypeList;

        private List<JobPackage> _jobPackageList;

        private GlobalUtilities()
        {
        }

        public static GlobalUtilities GetInstance()
        {
            return _instance;
        }

        private static List<JobPackage> BuildAvailableJobPackageList()
        {
            List<JobPackage> result = new List<JobPackage>();
            result.Add(new JobPackage { JobPackageID = 1, DName = "Single-Post - $99 per job, total $99", Name = "Single Post", PricePerJob = 99, PercentOff = 0, JobCredits = 1, Total = 99 });
            result.Add(new JobPackage { JobPackageID = 2, DName = "3-Posts ----- $89 per job, total $267 (10% off)", Name = "3 Posts", PricePerJob = 89, PercentOff = 10, JobCredits = 3, Total = 267 });
            result.Add(new JobPackage { JobPackageID = 3, DName = "5-Posts ----- $79 per job, total $395 (20% off)", Name = "5 Posts", PricePerJob = 79, PercentOff = 20, JobCredits = 5, Total = 395 });
            result.Add(new JobPackage { JobPackageID = 4, DName = "10-Posts ---- $69 per job, total $690 (30% off)", Name = "10 Posts", PricePerJob = 69, PercentOff = 30, JobCredits = 10, Total = 690 });
            result.Add(new JobPackage { JobPackageID = 5, DName = "20-Posts ---- $59 per job, total $1180 (40% off)", Name = "20 Posts", PricePerJob = 59, PercentOff = 40, JobCredits = 20, Total = 1180 });
            result.Add(new JobPackage { JobPackageID = 6, DName = "50-Posts ---- $49 per job, total $2450 (50% off)", Name = "50 Posts", PricePerJob = 49, PercentOff = 50, JobCredits = 50, Total = 2450 });
            result.Add(new JobPackage { JobPackageID = 7, DName = "100-Posts --- $39 per job, total $3900 (60% off)", Name = "100 Posts", PricePerJob = 39, PercentOff = 60, JobCredits = 100, Total = 3900 });
            return result;
        }

        //methods
        public void Init()
        {
            //initialize all static data here
            _stripePublishableKey = ConfigurationManager.AppSettings["StripePublishableKey"];
            _connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            _debugMode = Convert.ToBoolean(ConfigurationManager.AppSettings["DebugMode"]);
            _minifyJsCssFiles = Convert.ToBoolean(ConfigurationManager.AppSettings["MinifyJsCssFiles"]);
            _needTiming = Convert.ToBoolean(ConfigurationManager.AppSettings["NeedTiming"]);
            _minificationFilePathPrefix = ConfigurationManager.AppSettings["MinificationFilePathPrefix"];
            _backgroundTaskExecutionKey = ConfigurationManager.AppSettings["BackgroundTaskExecutionKey"];

            _testEmail = Convert.ToBoolean(ConfigurationManager.AppSettings["TestEmail"]);
            _testEmailFrom = ConfigurationManager.AppSettings["TestEmailFrom"].ToString();
            _testEmailTo = ConfigurationManager.AppSettings["TestEmailTo"].ToString();

            _emailServerHost = ConfigurationManager.AppSettings["EmailServerHost"].ToString();
            _emailCredentialUserName = ConfigurationManager.AppSettings["EmailCredentialUserName"].ToString();
            _emailCredentialPassword = ConfigurationManager.AppSettings["EmailCredentialPassword"].ToString();
            _email_BCC = ConfigurationManager.AppSettings["Email_BCC"].ToString();
            _email_General = ConfigurationManager.AppSettings["Email_General"].ToString();
            _email_NoReply = ConfigurationManager.AppSettings["Email_NoReply"].ToString();
            _email_Support = ConfigurationManager.AppSettings["Email_Support"].ToString();
            _email_DisplayName = ConfigurationManager.AppSettings["Email_DisplayName"].ToString();

            //[NOTE]: if you need query data from db here, you should specify connectionstring, otherwise it has exception when you compile code in release mode.
            _jobExpList = SqlUtilities.Query<JobExp>(_connectionString, ConstUtils.SP_GetJobExpList, null, null, null).ToList();  //DataUtils.GetList<JobExp>(ConstUtils.SP_GetJobExpList);
            _jobTypeList = SqlUtilities.Query<JobType>(_connectionString, ConstUtils.SP_GetJobTypeList, null, null, null).ToList(); //DataUtils.GetList<JobType>(ConstUtils.SP_GetJobTypeList);
            _coTypeList = SqlUtilities.Query<CoType>(_connectionString, ConstUtils.SP_GetCoTypeList, null, null, null).ToList(); //DataUtils.GetList<CoType>(ConstUtils.SP_GetCoTypeList);

            _jobPackageList = BuildAvailableJobPackageList();
        }

        //properties
        public string StripePublishableKey { get { return _stripePublishableKey; } }

        public string ConnectionString { get { return _connectionString; } }
        public Boolean MinifyJsCssFiles { get { return _minifyJsCssFiles; } }
        public Boolean DebugMode { get { return _debugMode; } }
        public Boolean NeedTiming { get { return _needTiming; } }
        public string MinificationFilePathPrefix { get { return _minificationFilePathPrefix; } }
        public string BackgroundTaskExecutionKey { get { return _backgroundTaskExecutionKey; } }

        public Boolean TestEmail { get { return _testEmail; } }
        public string TestEmailFrom { get { return _testEmailFrom; } }
        public string TestEmailTo { get { return _testEmailTo; } }

        public string EmailServerHost {get {return _emailServerHost;}}
        public string EmailCredentialUserName { get { return _emailCredentialUserName; } }
        public string EmailCredentialPassword { get { return _emailCredentialPassword; } }
        public string Email_BCC { get{return _email_BCC;}}
        public string Email_General {get {return _email_General;}}
        public string Email_NoReply {get {return _email_NoReply;}}
        public string Email_Support { get { return _email_Support; } }
        public string Email_DisplayName { get { return _email_DisplayName; } }

        public List<JobExp> JobExpList { get { return _jobExpList; } }
        public List<JobType> JobTypeList { get { return _jobTypeList; } }
        public List<CoType> CoTypeList { get { return _coTypeList; } }
        public List<JobPackage> JobPackageList { get { return _jobPackageList; } }

        public string GetJobExpDName(int jobExpID)
        {
            var jobexp = JobExpList.Find(x => x.JobExpID == jobExpID);
            return jobexp != null ? jobexp.DName : string.Empty;
        }

        public string GetJobTypeDName(int jobTypeID)
        {
            var jobType = JobTypeList.Find(x => x.JobTypeID == jobTypeID);
            return jobType != null ? jobType.DName : string.Empty;
        }

        public string GetCoTypeDName(int coTypeID)
        {
            var coType = CoTypeList.Find(x => x.CoTypeID == coTypeID);
            return coType != null ? coType.DName : string.Empty;
        }
    }
}